//
//  PHWPTipsViewController.m
//  PhnixHome
//
//  Created by Levy Xu on 2018/3/3.
//  Copyright © 2018年 Levy Xu. All rights reserved.
//

#import "PHWPTipsViewController.h"
#import "PHAddUtils.h"
#import "PHWiFiConnectViewController.h"

#define RippleAnimationDuration 2.0

@interface PHWPTipsViewController ()
@property (strong, nonatomic) UILabel *label1;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *label2;
@property (nonatomic, strong) QMUIFillButton *nextBtn;
@end

@implementation PHWPTipsViewController {
    UIView *_wrapView;
    UIImageView *_circleIV;
    UIImageView *_handIV;
    UIBezierPath *_initPath;
    UIBezierPath *_finalPath;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [NNCDC addObserver:self selector:@selector(beginAnimation) name:UIApplicationWillEnterForegroundNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    [NNCDC  removeObserver:self];
}

- (void)initSubviews {
    [super initSubviews];
    
    _label1 = [[QMUILabel alloc] init];
    _label1.font = CodeFontMake(14);
    _label1.textColor = UIColorGray3;
    _label1.textAlignment = NSTextAlignmentCenter;
    _label1.numberOfLines = 0;
    _label1.text = NSLocalizedString(@"tips_water_purifier_wifi_1", nil);
    [self.myView addSubview:_label1];
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    if (GPHAddUtils.selectedIndex == 2) {
        _imageView.image = UIImageMake(@"wifi_i8");
    } else {
        _imageView.image = UIImageMake(@"wifi_R8");
    }
    [self.myView addSubview:_imageView];
    
    _wrapView = [[UIView alloc] init];
    [_imageView addSubview:_wrapView];
    
    _circleIV = [[UIImageView alloc] initWithImage:UIImageMake(@"wifi_circle")];
    _circleIV.contentMode = UIViewContentModeScaleAspectFill;
    _circleIV.clipsToBounds = YES;
    _circleIV.layer.cornerRadius = CGRectGetMidX([self rippleRect]);
    [_wrapView addSubview:_circleIV];
    
    _handIV = [[UIImageView alloc] initWithImage:UIImageMake(@"wifi_hand")];
    [_imageView addSubview:_handIV];
    
    _label2 = [[QMUILabel alloc] init];
    [_label2 qmui_setTheSameAppearanceAsLabel:_label1];
    _label2.numberOfLines = 0;
    _label2.text = NSLocalizedString(@"tips_water_purifier_wifi_2", nil);
    [self.myView addSubview:_label2];
    
    _nextBtn = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorRed];
    _nextBtn.titleLabel.font = CodeFontMake(16);
    [_nextBtn setTitle:NSLocalizedString(@"action_next", nil) forState:UIControlStateNormal];
    [_nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:_nextBtn];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    _initPath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset([self rippleRect], 5, 5)];
    
    CGFloat expand = CGRectGetMidX([self rippleRect]);
    _finalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset([self rippleRect], 1 - expand, 1 - expand)];
    
    [self beginAnimation];
}

- (void)viewDidLayoutSubviews {
    self.shouldShowMyView = YES;
    [super viewDidLayoutSubviews];
    
    CGFloat width = 300.0f;
    CGFloat gap = 20.0f;
    CGFloat margin = 30.0f;
    if (IS_35INCH_SCREEN) {
        width = 200.0f;
        gap = 10.0f;
        margin = 15;
    } else if (IS_40INCH_SCREEN) {
        width = 260.0f;
        gap = 10.0f;
    }
    
    _label1.frame = CGRectMake(margin, gap, self.myView.qmui_width - 2 * margin, 160);
    [_label1 sizeToFit];
    
    _imageView.frame = CGRectMake((self.myView.qmui_width - width) / 2, _label1.qmui_bottom + gap, width, width);
    _wrapView.frame = [self rippleRect];
    if (GPHAddUtils.selectedIndex == 2) {
        _wrapView.center = CGPointMake(0.311f * width, 0.749f * width);
    } else {
        _wrapView.center = CGPointMake(0.430f * width, 0.476f * width);
    }
    _circleIV.frame = _wrapView.bounds;
    _handIV.frame = CGRectMake(_wrapView.center.x - 31.0, _wrapView.center.y - 8.0f, 40, 50);
    
    _label2.frame = CGRectMake(margin, _imageView.qmui_bottom + gap, self.myView.qmui_width - 2 * margin, 160);
    [_label2 sizeToFit];
    
    _nextBtn.frame = CGRectMake(30, _label2.qmui_bottom + gap + 5, self.myView.qmui_width - 60, 40);
}

-(void)nextAction:(id)sender {
    PHWiFiConnectViewController *wifiVC = [[PHWiFiConnectViewController alloc] init];
    [self.navigationController pushViewController:wifiVC animated:YES];
}

-(void)setupNavigationItems {
    [super setupNavigationItems];
    self.title = GPHAddUtils.selectedTitle;
}

#pragma mark - Animation

-(CGRect)rippleRect {
    if (GPHAddUtils.selectedIndex == 2) {
        return (CGRect){ 0, 0, 50, 50 };
    } else {
        return (CGRect){ 0, 0, 40, 40 };
    }
}

- (void)beginAnimation {
    [self animationReplicatorAvatarInView:_wrapView animated:YES];
    [_wrapView bringSubviewToFront:_circleIV];
}

- (void)animationReplicatorAvatarInView:(UIView *)view animated:(BOOL)animated {
    
    NSMutableArray *_layers = [[NSMutableArray alloc] init];
    NSInteger count = view.layer.sublayers.count;
    for (int i = 0; i < count; i++) {
        if ([view.layer.sublayers[i] isKindOfClass:[CAReplicatorLayer class]]) {
            [_layers addObject:view.layer.sublayers[i]];
            [view.layer.sublayers[i] setHidden:YES];
        }
    }
    count = _layers.count;
    for (int i = 0; i < count; i++) {
        [_layers[i] removeFromSuperlayer];
    }
    
    if (!animated) {
        return;
    }
    
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.instanceCount = 3;
    replicatorLayer.instanceDelay = RippleAnimationDuration / 3;
    replicatorLayer.backgroundColor = UIColorClear.CGColor;
    [view.layer addSublayer:replicatorLayer];
    
    CAShapeLayer *layer = [self animationLayerWithPath:_initPath];
    layer.frame = [self rippleRect];
    [replicatorLayer addSublayer:layer];
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    pathAnimation.fromValue = (id)_initPath.CGPath;
    pathAnimation.toValue = (id)_finalPath.CGPath;
    
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = @1;
    opacityAnimation.toValue = @0;
    
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = @[pathAnimation, opacityAnimation];
    groupAnimation.duration = RippleAnimationDuration;
    groupAnimation.repeatCount = HUGE_VALF;
    
    [layer addAnimation:groupAnimation forKey:nil];
}

- (CAShapeLayer *)animationLayerWithPath:(UIBezierPath *)path {
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.strokeColor = UIColorBlue.CGColor;
    layer.fillColor = UIColorClear.CGColor;
    layer.lineWidth = 1;
    return layer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
